package com.yeyks.controller.admin;

import com.google.common.collect.Lists;
import com.yeyks.account.dal.domain.AccountInfo;
import com.yeyks.annotation.AdminAppLog;
import com.yeyks.common.appcommon.dto.account.AppAccountDTO;
import com.yeyks.common.appcommon.dto.room.*;
import com.yeyks.common.appcommon.param.room.*;
import com.yeyks.common.appcommon.result.CommonResult;
import com.yeyks.common.base.RoomInfoDevicesJson;
import com.yeyks.common.base.RoomInfoRoomInfoJson;
import com.yeyks.common.dto.mq.EsUpdateMessage;
import com.yeyks.common.utils.apputil.bean.MyBeanUtils;
import com.yeyks.commonReference.append.rocketmq.service.producer.MQProducerService;
import com.yeyks.commonReference.service.hotel.RoomInfoService;
import com.yeyks.controller.BaseController;
import com.yeyks.hotel.service.RoomCalendarInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

import static com.yeyks.common.constants.SysLogMsg.*;

/**
 * @author Administrator
 */
@Api(tags = "AppRoomController_客房管理接口", description = "客房管理相关接口[新建, 编辑, 列表]")
@RestController
@RequestMapping("/admin/room")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class AppRoomController extends BaseController {


    private final RoomCalendarInfoService roomCalendarInfoService;

    private final RoomInfoService roomInfoService;

    private final MQProducerService producerService;

    /**
     * 查询客房(根据酒店ID查询)
     */

    @ApiOperation("查询客房(根据酒店ID查询)[兼容1.7.0优享客房]")
    @PostMapping("/list")
    public CommonResult<List<AppRoomInfoDTO>> list(@Valid QueryRoomParam queryRoomParam) {

        AppAccountDTO appAccountDTO = validToken(queryRoomParam.getToken(),
                queryRoomParam.getVersion(), queryRoomParam.getPlatform());

        return roomInfoService.pageList(queryRoomParam, MyBeanUtils.convert(appAccountDTO, AccountInfo.class));
        //        //return appRoomInfoService.list(queryRoomParam);
    }

    /**
     * 新建客房
     */
    @AdminAppLog(value = ROOM_ADD)
    @ApiOperation("新建客房接口")
    @PostMapping("/add")
    public CommonResult addRoom(@Valid AddRoomInfoParam addRoomInfoParam) {

        AppAccountDTO appAccountDTO = validToken(addRoomInfoParam.getToken(),
                addRoomInfoParam.getVersion(), addRoomInfoParam.getPlatform());

        return roomInfoService.addRoom(addRoomInfoParam, MyBeanUtils.convert(appAccountDTO, AccountInfo.class));
    }

    @ApiOperation("获取房态信息")
    @PostMapping("/get/calendar")
    public CommonResult<List<AppRoomCalendarDTO>> getRoomCalendar(@Valid GetRoomCalendarParam param) {

        AppAccountDTO appAccountDTO = validToken(param.getToken(), param.getVersion(), param.getPlatform());

        return roomCalendarInfoService.getRoomCalendar(param, appAccountDTO);
    }

    @AdminAppLog(value = ROOM_UPDATE_CALENDAR)
    @ApiOperation("更新房态信息")
    @PostMapping("/update/calendar")
    public CommonResult updateRoomCalendar(@Valid UpdateRoomCalendarParam param) {

        AppAccountDTO appAccountDTO = validToken(param.getToken(), param.getVersion(), param.getPlatform());

        CommonResult commonResult = roomCalendarInfoService.updateRoomCalendar(param, appAccountDTO);
        if(commonResult.getCode() == CommonResult.SUCCESS){
            List<String> sourceIds = Lists.newArrayList();
            sourceIds.add(String.valueOf(param.getRoomId()));
            //更新酒店的相关备注信息，发送Mq更新ES
            producerService.sendEsUpdate(new EsUpdateMessage().setType("room")
                    .setSourceType("yyks").setSourceIds(sourceIds),0);
        }
        return commonResult;
    }


    @ApiOperation("获取单个客房信息（兼容1.7.0优享VIP）")
    @PostMapping("/get")
    public CommonResult<AppRoomDetailInfoDTO> getRoom(@Valid GetRoomParam getRoomParam) {

        AppAccountDTO appAccountDTO = validToken(getRoomParam.getToken(),
                getRoomParam.getVersion(), getRoomParam.getPlatform());

        return roomInfoService.getRoom(getRoomParam, MyBeanUtils.convert(appAccountDTO, AccountInfo.class));
        //return appRoomInfoService.getRoom(getRoomParam);
    }

    @AdminAppLog(value = ROOM_UPGRADE)
    @ApiOperation("升级优享客房（1.7.0）")
    @PostMapping("/upgrade/min/vip")
    public CommonResult upgradeJipRoom(@Valid UpdateUpgradeMinVipParam param){

        AppAccountDTO appAccountDTO = validToken(param.getToken(),
                param.getVersion(), param.getPlatform());

        return roomInfoService.upgradeJipRoom(param, MyBeanUtils.convert(appAccountDTO, AccountInfo.class));
    }

    /**
     * 编辑客房
     */
    @AdminAppLog(value = ROOM_UPDATE_MSG)
    @ApiOperation("编辑客房接口")
    @PostMapping("/update")
    public CommonResult updateRoom(@Valid UpdateRoomInfoParam updateRoomInfoParam) {

        AppAccountDTO appAccountDTO = validToken(updateRoomInfoParam.getToken(),
                updateRoomInfoParam.getVersion(), updateRoomInfoParam.getPlatform());

        return roomInfoService.updateRoom(updateRoomInfoParam, MyBeanUtils.convert(appAccountDTO, AccountInfo.class));
        //return appRoomInfoService.updateRoom(updateRoomInfoParam);
    }

    @AdminAppLog(value = ROOM_UPDATE_MSG)
    @ApiOperation("编辑任意客房接口【兼容1.7.0（更新默认信息）】")
    @PostMapping("/update/any/param")
    public CommonResult updateAnyParamRoom(@Valid UpdateRoomInfoParam updateRoomInfoParam) {

        AppAccountDTO appAccountDTO = validToken(updateRoomInfoParam.getToken(),
                updateRoomInfoParam.getVersion(), updateRoomInfoParam.getPlatform());

        return roomInfoService.updateAnyParamRoom(updateRoomInfoParam, MyBeanUtils.convert(appAccountDTO, AccountInfo.class));
    }

    @AdminAppLog(value = ROOM_UPDATE_VALID)
    @ApiOperation("上下架接口")
    @PostMapping("/valid")
    public CommonResult validRoom(@Valid UpdateValidRoomParam updateValidRoomParam) {

        AppAccountDTO appAccountDTO = validToken(updateValidRoomParam.getToken(),
                updateValidRoomParam.getVersion(), updateValidRoomParam.getPlatform());

        return roomInfoService.validRoom(updateValidRoomParam, MyBeanUtils.convert(appAccountDTO, AccountInfo.class));
    }

    @AdminAppLog(value = ROOM_UPDATE_VALID)
    @ApiOperation("上下架接口【优享VIP】（1.7.0）")
    @PostMapping("/valid/min/vip")
    public CommonResult validRoomMinVip(@Valid UpdateValidRoomParam updateValidRoomParam) {

        AppAccountDTO appAccountDTO = validToken(updateValidRoomParam.getToken(),
                updateValidRoomParam.getVersion(), updateValidRoomParam.getPlatform());

        return roomInfoService.validRoomMinVip(updateValidRoomParam, MyBeanUtils.convert(appAccountDTO, AccountInfo.class));
    }

    @AdminAppLog(value = ROOM_DEL)
    @ApiOperation("删除客房信息")
    @PostMapping("/del")
    public CommonResult delRoom(@Valid DelRoomParam delRoomParam) {

        AppAccountDTO appAccountDTO = validToken(delRoomParam.getToken(),
                delRoomParam.getVersion(), delRoomParam.getPlatform());

        return roomInfoService.delRoom(delRoomParam, MyBeanUtils.convert(appAccountDTO, AccountInfo.class));
    }

    @ApiOperation("获取客房床型接口(1.7.0对象改动)")
    @PostMapping("/bed/type/new")
    public CommonResult<List<RoomInfoRoomInfoJson>> getRoomInfoJsonList(@RequestParam(value = "token", required = true) String token,
                                                                   @RequestParam(value = "version", required = false) String version,
                                                                   @RequestParam(value = "platform", required = false) String platform) {

        AppAccountDTO appAccountDTO = validToken(token, version, platform);

        return roomInfoService.getRoomInfoJsonList(MyBeanUtils.convert(appAccountDTO, AccountInfo.class));
    }

    @ApiOperation("获取客房服务设备接口(1.7.0对象改动)")
    @PostMapping("/get/devices/new")
    public CommonResult<List<RoomInfoDevicesJson>> getDevicesJsonList(@RequestParam(value = "token", required = true) String token,
                                                                  @RequestParam(value = "version", required = false) String version,
                                                                  @RequestParam(value = "platform", required = false) String platform) {
        AppAccountDTO appAccountDTO = validToken(token, version, platform);

        return roomInfoService.getDevicesJsonList(MyBeanUtils.convert(appAccountDTO, AccountInfo.class));
    }

    @ApiOperation("获取客房床型接口(old)")
    @PostMapping("/bed/type")
    public CommonResult<List<AppRoomBedTypeDTO>> getBedTypeList(@RequestParam(value = "token", required = true) String token,
                                                                @RequestParam(value = "version", required = false) String version,
                                                                @RequestParam(value = "platform", required = false) String platform) {

        AppAccountDTO appAccountDTO = validToken(token, version, platform);

        return roomInfoService.getBedTypeList(MyBeanUtils.convert(appAccountDTO, AccountInfo.class));
    }

    @ApiOperation("获取客房服务设备接口(old)")
    @PostMapping("/get/devices")
    public CommonResult<List<AppRoomDevicesDTO>> getDevicesList(@RequestParam(value = "token", required = true) String token,
                                                                @RequestParam(value = "version", required = false) String version,
                                                                @RequestParam(value = "platform", required = false) String platform) {
        AppAccountDTO appAccountDTO = validToken(token, version, platform);

        return roomInfoService.getDevicesList(MyBeanUtils.convert(appAccountDTO, AccountInfo.class));
    }
}
